{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from random import choice, sample\n",
    "from itertools import permutations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## common"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def score(x, y):\n",
    "    bulls = sum(i == j for i, j in zip(x, y))\n",
    "    cows = len(set(x) & set(y)) - bulls\n",
    "    return bulls, cows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## player 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def player1(player2):\n",
    "    secret = sample(range(10), 4)\n",
    "    \n",
    "    tip = next(player2)\n",
    "    while True:\n",
    "        b, c = score(secret, tip)\n",
    "        if b < 4:\n",
    "            print(b, 'bulls', c, 'cows')\n",
    "            tip = player2.send((b, c))\n",
    "        else:\n",
    "            print('you won')\n",
    "            break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## player 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def player2():\n",
    "    tips = list(permutations(range(10), 4))\n",
    "    \n",
    "    while True:\n",
    "        tip = choice(tips)\n",
    "        print(tip, '?')\n",
    "        bc = yield tip\n",
    "        tips = [i for i in tips if score(i, tip) == bc]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## game"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 8, 3, 9) ?\n",
      "1 bulls 1 cows\n",
      "(9, 1, 3, 0) ?\n",
      "1 bulls 1 cows\n",
      "(5, 9, 7, 0) ?\n",
      "1 bulls 1 cows\n",
      "(6, 9, 3, 7) ?\n",
      "3 bulls 0 cows\n",
      "(2, 9, 3, 7) ?\n",
      "3 bulls 0 cows\n",
      "(4, 9, 3, 7) ?\n",
      "you won\n"
     ]
    }
   ],
   "source": [
    "player1(player2())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3, 6, 2, 8) ?\n",
      "0 bulls 1 cows\n",
      "(7, 0, 8, 4) ?\n",
      "0 bulls 2 cows\n",
      "(2, 7, 5, 0) ?\n",
      "0 bulls 1 cows\n",
      "(8, 5, 4, 9) ?\n",
      "1 bulls 1 cows\n",
      "(4, 3, 7, 9) ?\n",
      "0 bulls 3 cows\n",
      "(6, 9, 4, 7) ?\n",
      "2 bulls 0 cows\n",
      "(0, 9, 4, 3) ?\n",
      "you won\n"
     ]
    }
   ],
   "source": [
    "player1(player2())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
